package com.suql.kotlin.activity

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.widget.Toast
import com.suql.kotlin.R

import com.suql.networktools.bean.UserInfo
import com.suql.networktools.net.contract.LoginContract
import com.suql.networktools.net.presenter.LoginPresenter
import kotlinx.android.synthetic.main.activity_login.*

/**
 * @ClassName: LoginActivity
 * @Dscription: 登陆页面
 * @Author: su qinglin
 * @Date: 2018/7/26 16:46
 */
class LoginActivity : BaseActivity(), View.OnClickListener, LoginContract.IView {

    private lateinit var mPresenter: LoginContract.IPresenter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)
        mPresenter = LoginPresenter(this)
        btn_login.setOnClickListener(this)
    }

    override fun onClick(view: View) {

        when (view.id) {

            R.id.btn_login -> {
                val userName = edt_login_userName.text.toString()
                val password = edt_login_password.text.toString()
                if (TextUtils.isEmpty(userName)) {
                    Toast.makeText(this, getString(R.string.user_name_hint), Toast.LENGTH_SHORT).show()
                    return
                }
                if (TextUtils.isEmpty(password)) {
                    Toast.makeText(this, getString(R.string.password_hint), Toast.LENGTH_SHORT).show()
                    return
                }

                login(userName, password)
            }
        }
    }

    private fun login(userName: String, password: String) {

//        mPresenter.login(userName, password)
        startActivity(Intent(this, MainActivity::class.java))
        finish()
    }

    override fun loginSuccess(userInfo: UserInfo?, message: String?) {

        if (message != null) {
            showToast(message)
        }
        startActivity(Intent(this, MainActivity::class.java))
        finish()
    }

    override fun loginFail(message: String?) {

        if (message != null) {
            showToast(message)
        }
    }
}
